package Sort;

import java.util.Arrays;

public class ShellSort {
    public static void shellSort(int[] array){
        int gap = array.length/2;
        while(gap>1){
            gap = gap/2;
            shell1(array,gap);
        }
    }

    public static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >=0 ; j-= gap) {
                if(array[j]>tmp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    public static void shell1(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            for (int j = i-gap; j >=0 ; j-=gap) {
                if(array[j]>array[j+gap]){
                    int tmp = array[j];
                    array[j] = array[j+gap];
                    array[j+gap]  = tmp;
                }else{
                    break;
                }
            }
        }
    }
    public static void  TestSort(int[] array){
        int[] tmp = Arrays.copyOf(array,array.length);
        long time1 = System.currentTimeMillis();
        shellSort(tmp);
        long time2 = System.currentTimeMillis();
        System.out.println("排序用时："+(time2-time1));

    }

    //创建最坏情况的排序
    public static void createArray(int[] array){
        for (int i = 0; i < array.length ; i++) {
            array[i] = array.length-i;
        }
    }

    public static void main(String[] args) {
        int[] array = new int[10_0000];
        //完全逆序
        createArray(array);
        TestSort(array);
    }
}
